package com.example;

import java.util.Stack;

/**
 * Created by xiao_tan on 17/4/6.
 */

public class Interviews {

    static {
        System.out.println("Interviews,静态代码块被执行了");
    }


    public static void main(String[] args) {

        System.out.println(reverse1("I am an engineer"));
        System.out.println(reverse2("I am an teacher"));
        System.out.println(isMopNum(233333) && isPrimeNum(233333));
        System.out.println(isMopNumByRecursive(433333));
        System.out.println(isMopNum(233333333));
        System.out.println(isMopNum(533333));
    }


    public static String reverse1(String inputStr) {
        if (inputStr == null || inputStr.length() == 0) {
            return null;
        }

        String[] split = inputStr.split(" ");
        if (split.length <= 1) {
            return inputStr;
        }

        StringBuilder sb = new StringBuilder(inputStr);
        String reverseStr = sb.reverse().toString();

        StringBuilder outputSb = new StringBuilder();
        String[] splitReverse = reverseStr.split(" ");
        for (String str : splitReverse) {
            StringBuilder sBuilder = new StringBuilder(str);
            outputSb.append(sBuilder.reverse().toString()).append(" ");
        }
        outputSb.delete(outputSb.length() - 1, outputSb.length());

        return outputSb.toString();
    }

    public static String reverse2(String inputStr) {
        if (inputStr == null || inputStr.length() == 0) {
            return null;
        }

        String[] split = inputStr.split(" ");
        if (split.length <= 1) {
            return inputStr;
        }
        Stack<String> stack = new Stack<>();
        for (String str : split) {
            stack.push(str);
        }
        StringBuilder outputSb = new StringBuilder();
        while (!stack.isEmpty()) {
            outputSb.append(stack.pop()).append(" ");
        }
        outputSb.delete(outputSb.length() - 1, outputSb.length());
        return outputSb.toString();
    }

    public static boolean isMopNumByRecursive(int number) {
        //233,2333,2333
        if (number <= 0) {
            return false;
        }
        if (number < 10) {
            return number == 2;
        } else {
            return (number % 10 == 3) && isMopNumByRecursive(number / 10);
        }
    }


    public static boolean isMopNum(int number) {
        //233,2333,2333
        if (number <= 0) {
            return false;
        }
        int remainder;
        while (number > 10) {
            remainder = number % 10;
            if (remainder != 3) {
                return false;
            }
            number = number / 10;
        }
        return number == 2;
    }

    public static boolean isPrimeNum(int number) {

        if (number <= 1) {
            return false;
        }
        int i = number / 2;
        for (int j = 2; j <= i; j++) {
            if (number % j == 0) {
                System.out.println(number + "/" + j + " = " + (number / j));
                return false;
            }
        }
        return true;
    }


}
